package com.fscp.oauth2.server.controller;

import com.fscp.commons.model.domain.ResultInfo;
import com.fscp.commons.model.domain.SignInIdentity;
import com.fscp.commons.model.vo.SignInDinerInfo;
import com.fscp.commons.utils.ResultInfoUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 用户中心
 */
@RestController
public class UserController {

    @Resource
    private HttpServletRequest request;

    @Resource
    private RedisTokenStore redisTokenStore;

    // 路由资源需要放行； 在资源服务配置类中 ResourceServerConfig
    @GetMapping("user/me")
    public ResultInfo getCurrentUser(Authentication authentication) {
        // 获取登录用户的信息，然后设置
        SignInIdentity signInIdentity = (SignInIdentity) authentication.getPrincipal();
        // 转为前端可用的视图对象
        SignInDinerInfo dinerInfo = new SignInDinerInfo();
        BeanUtils.copyProperties(signInIdentity, dinerInfo);

        return ResultInfoUtil.buildSuccess(request.getServletPath(), dinerInfo);
    }

    // localhost:8082/user/me?access_token=f99829a9-bd26-4456-83bf-c817a0e7c47c
    // localhost:8082/user/me        authorization.Bearer Token = f99829a9-bd26-4456-83bf-c817a0e7c47c

    /**
     * 安全退出
     *
     * @param access_token
     * @param authentication
     * @return
     */
    @GetMapping("user/logout")
    public ResultInfo logout(String access_token, Authentication authentication) {
        // 判断 access_token 是否为空，为空将 authorization 献给access_token
        if (StringUtils.isBlank(access_token)) {
            access_token = ((OAuth2AuthenticationDetails) authentication.getDetails()).getTokenValue();
        }
        // 判断 authorization 是否为空
        if (StringUtils.isBlank(access_token)) {
            // 没有令牌信息， 告知退出成功 --- 友好交互
            return ResultInfoUtil.buildSuccess(request.getServletPath(), "退出成功！");
        }
        // 判断 bearer token 是否为空
        if (access_token.toLowerCase().contains("bearer ".toLowerCase())) {
            // 将 bearer token 的前半部分 bearer  替换为空，剩下 token
            access_token = access_token.toLowerCase().replace("bearer ", "");
        }
        // 清除 redis token 信息，  先读 redis 库
        OAuth2AccessToken oAuth2AccessToken = redisTokenStore.readAccessToken(access_token);
        if (oAuth2AccessToken != null) {
            redisTokenStore.removeAccessToken(oAuth2AccessToken);
            OAuth2RefreshToken refreshToken = oAuth2AccessToken.getRefreshToken();
            redisTokenStore.removeRefreshToken(refreshToken);
        }
        return ResultInfoUtil.buildSuccess(request.getServletPath(), "退出成功！");
    }
}
